#include "adjacency.h"

AdjList *CreateAdjList(int n)
{
  AdjList *A=NULL;

   A = (AdjList *)calloc(1,sizeof(AdjList ));
   A->adj = (AdjNode *) calloc(n,sizeof(AdjNode));
   A->n = n;

  return(A);
} 

AdjList *ReadAdjList(char *filename)
{
  FILE *fp=fopen(filename,"r");
  AdjList *A=NULL;
  int i,n;

   fscanf(fp,"%d",&n); 
   A = CreateAdjList(n);
   
   for (i=0; i < A->n; i++) {
     fscanf(fp,"%d %d %f",&A->adj[i].dx,&A->adj[i].dy,&A->adj[i].w); 
   }
   fclose(fp);
   return(A);
}

void DestroyAdjList(AdjList **A)
{
  AdjList *aux=*A;

 if (aux!=NULL){
    free(aux->adj);
    free(aux);
    *A = NULL;
  }
}

AdjList *CircularGaussian(float radius)
{
  AdjList *A=NULL;
  int dx,dy,n,i;
  
  n=0;
  for (dy=0; dy <= (int)radius; dy++)
    for (dx=0; dx <= (int)radius; dx++)
      if ((dx*dx + dy*dy)<=(radius*radius))
	n++;
  n++;
  A = CreateAdjList(n);
  i=0;
  for (dy=0; dy <= (int)radius; dy++)
    for (dx=0; dx <= (int)radius; dx++)
      if ((dx*dx + dy*dy)<=(radius*radius)){
	A->adj[i].dx = dx;
	A->adj[i].dy = dy;
	A->adj[i].w  = exp(-(dx*dx + dy*dy)/(radius*radius));
	i++;
      }

  return(A);
}
